/**
 *
 * 漫游路径
 * @param       viewer                   mars3d实例
 * @param       BQGIS_NavigationPath     navigationPath实例
 * 
 * @function    playNavigationPath       播放/恢复
 * @function    pauseNavigationPath      暂停
 * @function    stopNavigationPath       停止
 * @function    proceedNavigationPath    恢复
 * @function    changeSpeed              更改当前播放路径的速度  
 * 
 */

export function BQGIS_initNavigationPathPlayer(viewer = window.viewer, endCallBack = () => {}) {
    if(!viewer.bq_navigationPathPlayer){
        viewer.bq_navigationPathPlayer = new BQGIS_navigationPathPlayer(viewer, endCallBack)
    }
    return viewer.bq_navigationPathPlayer
}


function BQGIS_navigationPathPlayer(viewer, endCallBack) {
    this.viewer = viewer
    this.FixedRoute = null
    this.navigationPath = null
    this.endCallBack = endCallBack
}

BQGIS_navigationPathPlayer.prototype.playNavigationPath = function (navigationPath, isKeyframe) {
    this.navigationPath = navigationPath
    if (this.FixedRoute) {
        if(this.FixedRoute.isPause){ // 暂停
            this.FixedRoute.proceed()
        }else if(!this.FixedRoute.isStart){ // 未播放状态
            this.removeFixedRoute()
            this.setFixedRoute(navigationPath, isKeyframe)
            this.FixedRoute.start()
        }
    }else{
        this.setFixedRoute(navigationPath, isKeyframe)
        this.FixedRoute.start()
    }
}

BQGIS_navigationPathPlayer.prototype.setFixedRoute = function (navigationPath, isKeyframe) {
    this.FixedRoute =  new mars3d.graphic.FixedRoute(this.getOption(navigationPath, isKeyframe))
    this.viewer.graphicLayer.addGraphic(this.FixedRoute)
    this.FixedRoute.on(mars3d.EventType.end,  this.endCallBack)
}

BQGIS_navigationPathPlayer.prototype.removeFixedRoute = function () {
    this.viewer.graphicLayer.removeGraphic(this.FixedRoute, true)
    this.FixedRoute.off()
    this.FixedRoute = null
}

BQGIS_navigationPathPlayer.prototype.pauseNavigationPath = function () {
    if (this.FixedRoute) {
        this.FixedRoute.pause()
    }
}

BQGIS_navigationPathPlayer.prototype.stopNavigationPath = function () {
    if (this.FixedRoute) {
        this.FixedRoute.stop()
        this.removeFixedRoute()
    }
}

BQGIS_navigationPathPlayer.prototype.proceedNavigationPath = function () {
    if (this.FixedRoute) {
        this.FixedRoute.proceed()
    }
}

BQGIS_navigationPathPlayer.prototype.setfirstPerson = function (isfirstPerson) {
    this.navigationPath.isfirstPerson = isfirstPerson
    if (this.FixedRoute) {
        const cameraOptions = isfirstPerson ? {
            type: "dy",
            followedX: 50,
            followedZ: 10
        } : {
            type: "gs",
            radius: 100,
            followedX: 50,
            followedZ: 10
        }
        this.FixedRoute.setCameraOptions(cameraOptions)
    }
}

BQGIS_navigationPathPlayer.prototype.getOption = function (navigationPath, isKeyframe) {
    const option = {
        name: navigationPath.name,
        speed: Number(navigationPath.speed) * 3.6,
        positions: navigationPath.positions,
        showStop: false,
        autoStop: true,
        clockLoop: false, // 是否循环播放
        interpolation: false, // setInterpolationOptions插值
        offsetHeight:  navigationPath.offsetHeight,
        // model: { // 显示模型
        //     url: navigationPath.url,
        //     show: navigationPath.isModelShow
        // },
        billboard:{
            image: '/images/svg/panorama.svg',
            width: 30,
            height: 30,
        },
        path: {
            color: "#ffff00",
            width: 3,
            show: navigationPath.isPathShow
        },
    }
    if(isKeyframe){
        option.timeField = 'time'
        let date = new Date().getTime()
        option.positions.forEach((item, index) => {
            if(item.speed || item.speed === 0){
                date = date + item.speed * 1000
                item.time = formatDateTime(date)
            }else{
                delete option.timeField
            }
        });
    }
    option.camera = navigationPath.isfirstPerson ? {
        type: "dy",
        followedX: 50,
        followedZ: 10
    } : {
        type: "gs",
        radius: 100,
        followedX: 50,
        followedZ: 10
    }
    console.log(option,'option')
    return option
}

function formatDateTime(date) {
    var date = new Date(date);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    var second = date.getSeconds();
    second = second < 10 ? ('0' + second) : second;
    return y + '/' + m + '/' + d + ' ' + h + ':' + minute + ':' + second; 
}